namespace SmartSoft.Controls
{
    using SmartSoft;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    public class FormControl : Form
    {
        private IContainer components;
        private Keys ik_UserLastKey;
        private DataGridView oActiveGridBrowser;

        public FormControl()
        {
            this.InitializeComponent();
        }

        protected void ResizeForm()
        {
            Screen scrn = Screen.FromControl(this);
            if (scrn == null)
            {
                scrn = Screen.PrimaryScreen;
            }
            int deskHeight = scrn.Bounds.Height;
            int deskWidth = scrn.Bounds.Width;
            if (deskWidth < 1000)
            {
                this.Height = 577;
                this.Width = 810;
            }
            else
            {
                this.Width = 924;
                this.Location = new Point(this.Location.X - 57, this.Location.Y);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private object GetActiveValue()
        {
            Control userActiveControl = this.GetUserActiveControl();
            if ((userActiveControl is DataGridView) && ((userActiveControl as DataGridView).CurrentCell != null))
            {
                return (userActiveControl as DataGridView).CurrentCell.Value;
            }
            if (userActiveControl is TextBox)
            {
                return (userActiveControl as TextBox).Text;
            }
            return null;
        }

        public Control GetUserActiveControl()
        {
            Control activeControl = base.ActiveControl;
            while (activeControl is ContainerControl)
            {
                activeControl = (activeControl as ContainerControl).ActiveControl;
            }
            return activeControl;
        }

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormControl));
            this.SuspendLayout();
            // 
            // FormControl
            // 
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.KeyPreview = true;
            this.Name = "FormControl";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "FormControl";
            this.ResumeLayout(false);

        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            this.UserLastKey = keyData;
            if (((msg.Msg == 0x100) && (keyData == Keys.Return)) && !(this.GetUserActiveControl() is Button))
            {
                if (this.GetUserActiveControl() is TextBox)
                {
                    if ((this.GetUserActiveControl() as TextBox).Multiline)
                    {
                        return base.ProcessCmdKey(ref msg, keyData);
                    }
                    SendKeys.Send("{TAB}");
                }
                else
                {
                    SendKeys.Send("{TAB}");
                }
                return true;
            }
            if ((msg.Msg == 0x100) && (keyData == Keys.F5))
            {
                AppInstance.UserCalculator.CurrentControl = this.GetUserActiveControl();
                AppInstance.UserCalculator.SetTextValue(this.GetActiveValue());
                AppInstance.UserCalculator.Show();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        [Browsable(false)]
        public DataGridView ActiveGridBrowser
        {
            get
            {
                return this.oActiveGridBrowser;
            }
            set
            {
                if (!this.CheckSearchFocused && (this.oActiveGridBrowser != value))
                {
                    this.oActiveGridBrowser = value;
                }
            }
        }

        [Browsable(false)]
        private bool CheckSearchFocused
        {
            get
            {
                return ((this.GetUserActiveControl() != null) && (this.GetUserActiveControl() is TextBox));
            }
        }

        [Browsable(false)]
        public Keys UserLastKey
        {
            get
            {
                return this.ik_UserLastKey;
            }
            set
            {
                this.ik_UserLastKey = value;
            }
        }
    }
}

